package com.tedu;

import java.io.*;
import java.util.Scanner;
public class Test3 {
	public static void main(String[] args) throws IOException {
		method();
	}
	private static void method() throws IOException {
		System.out.println("输入一个文件名");
		File f = new File("D:\\"+new Scanner(System.in).nextLine());
		System.out.println(f);
		if (f.exists()){
			System.out.println("是否更换文件名？[y]更换，[n]不更换");
			String sc1= new Scanner(System.in).nextLine();
			if (sc1.equals("y")){
				method();
			} else if(sc1.equals("n")){
				System.out.println("继续写入文件？[y]重新[n]继续");
				String sa2  = new Scanner(System.in).nextLine();
				if ("y".equals(sa2)){
					System.out.println("现在输入要记录的内容,单独输入'exit'退出记事本");
					PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f,false),"UTF-8")),true);
					while (true) {
						String b  = new Scanner(System.in).nextLine();
						if(!b.equals("exit")) {
							pw.write(b);
						}else{
							System.out.println("写完了");
							break;
						}
					}
					pw.close();
				}else if("n".equals(sa2)){
					System.out.println("现在输入要记录的内容，单独输入'exit'退出记事本");
					PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f,true),"UTF-8")),true);
					while(true){
						String b  = new Scanner(System.in).nextLine();
						if (!b.equals("exit")){
							pw.write(b);
						}else {
							System.out.println("你已退出记事本");
							break;
						}
					}
					pw.close();
				}else {
					System.out.println("输入错误程序结束，下次记得输入y或者n");
				}
			}else{
				System.out.println("输入错误程序结束，下次记得输入y或则n");
			}
		}else {
			System.out.println("输入要记录的内容");
			File f1 = new File(String.valueOf(f));
			f1.createNewFile();
			PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f1,true),"UTF-8")),true);
			while (true){
				String sc3=new Scanner(System.in).nextLine();
				if (!"exit".equals(sc3)){
					pw.write(sc3);
				}else{
					System.out.println("写出完毕");
					break;
				}
			}
			pw.close();
		}
	}
}
